package cn.cloudreal.cbms.equip.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import cn.cloudreal.cbms.common.util.Constants;
import cn.cloudreal.cbms.common.util.DatadicContext;
import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.equip.service.EquipService;
import cn.cloudreal.cbms.vo.Dictionary;
import cn.cloudreal.cbms.vo.Equip;

import com.opensymphony.xwork2.ActionSupport;

public class EquipAction extends ActionSupport {
	/** serialVersionUID */
	private static final long serialVersionUID = 1L;

	/** 日志工具类实例 */
	private static final Logger log = Logger.getLogger(EquipAction.class);

	/** 服务 */
	private EquipService equipService;

	/** 当前页 */
	int currentPage;

	/** 分页对象 */
	PageBean page;

	/** 参数 */
	Integer id;
	String discriminate;
	String serialNum;
	String deviceType;
	String installMan;
	String installTime;
	String samId;
	String samType;
	String cityId;
	String lineId;
	String station;
	String supplier;
	String purchaseDate;
	String registerMan;
	String registerTime;
	String status;
	String comments;

	Equip equip;

	/** 列表 */
	List<Equip> equips = new ArrayList<Equip>();

	/** 字典 */
	List<Dictionary> deviceTypeSelect = new ArrayList<Dictionary>();
	List<Dictionary> samTypeSelect = new ArrayList<Dictionary>();
	List<Dictionary> cityIdSelect = new ArrayList<Dictionary>();
	List<Dictionary> lineIdSelect = new ArrayList<Dictionary>();
	List<Dictionary> stationSelect = new ArrayList<Dictionary>();
	List<Dictionary> statusSelect = new ArrayList<Dictionary>();

	public void setEquipService(EquipService equipService) {
		this.equipService = equipService;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public PageBean getPage() {
		return page;
	}

	public void setPage(PageBean page) {
		this.page = page;
	}

	public String getSerialNum() {
		return serialNum;
	}

	public void setSerialNum(String serialNum) {
		this.serialNum = serialNum;
	}

	public String getDeviceType() {
		return deviceType;
	}

	public void setDeviceType(String deviceType) {
		this.deviceType = deviceType;
	}

	public void setEquip(Equip equip) {
		this.equip = equip;
	}

	public Equip getEquip() {
		return equip;
	}

	public List<Equip> getEquips() {
		return equips;
	}

	public List<Dictionary> getDeviceTypeSelect() {
		return deviceTypeSelect;
	}

	public List<Dictionary> getSamTypeSelect() {
		return samTypeSelect;
	}

	public List<Dictionary> getCityIdSelect() {
		return cityIdSelect;
	}

	public List<Dictionary> getLineIdSelect() {
		return lineIdSelect;
	}

	public List<Dictionary> getStationSelect() {
		return stationSelect;
	}

	public List<Dictionary> getStatusSelect() {
		return statusSelect;
	}

	public void setDiscriminate(String discriminate) {
		this.discriminate = discriminate;
	}

	public void setInstallMan(String installMan) {
		this.installMan = installMan;
	}

	public void setInstallTime(String installTime) {
		this.installTime = installTime;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setSamId(String samId) {
		this.samId = samId;
	}

	public void setSamType(String samType) {
		this.samType = samType;
	}

	public void setCityId(String cityId) {
		this.cityId = cityId;
	}

	public void setLineId(String lineId) {
		this.lineId = lineId;
	}

	public void setStation(String station) {
		this.station = station;
	}

	public void setSupplier(String supplier) {
		this.supplier = supplier;
	}

	public void setPurchaseDate(String purchaseDate) {
		this.purchaseDate = purchaseDate;
	}

	public void setRegisterMan(String registerMan) {
		this.registerMan = registerMan;
	}

	public void setRegisterTime(String registerTime) {
		this.registerTime = registerTime;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	/**
	 * 列表
	 * 
	 * @return
	 */
	public String list() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter EquipAction list() method");
		}
		// 条件
		Map<Object, Object> condition = new HashMap<Object, Object>();
		condition.put("serialNum", serialNum);
		condition.put("deviceType", deviceType);
		// 分页对象初始化
		page = new PageBean(currentPage == 0 ? 1 : currentPage,
				Constants.PAGE_SIZE);
		// 查询总记录数
		int totalRecords = equipService.getEquipListCount(condition);
		//
		page.setTotalRecords(totalRecords);
		// 查询
		equips = equipService.getEquipList(condition, page);
		//
		for (Equip e : equips) {
			e.setCityIdText(DatadicContext.getInstance().getDatadicObject(null,
					e.getCityId()).getDatavalue());
			e.setLineIdText(DatadicContext.getInstance().getDatadicObject(null,
					e.getLineId()).getDatavalue());
			e.setStationText(DatadicContext.getInstance().getDatadicObject(
					null, e.getStation()).getDatavalue());
			e.setStatusText(DatadicContext.getInstance().getDatadicObject(null,
					e.getStatus()).getDatavalue());
			e.setDeviceTypeText(DatadicContext.getInstance().getDatadicObject(
					null, e.getDeviceType()).getDatavalue());
			e.setSamTypeText(DatadicContext.getInstance().getDatadicObject(
					null, e.getSamType()).getDatavalue());
		}
		//
		deviceTypeSelect = DatadicContext.getInstance().getDicDataListByCode(
				"SBLX");
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit EquipAction list() method");
		}
		return SUCCESS;
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter EquipAction delete() method");
		}
		// 删除
		equipService.deleteEquip(equip);
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit EquipAction delete() method");
		}
		return SUCCESS;
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public String initModify() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter EquipAction initModify() method");
		}
		//
		deviceTypeSelect = DatadicContext.getInstance().getDicDataListByCode(
				"SBLX");
		samTypeSelect = DatadicContext.getInstance().getDicDataListByCode(
				"SAMLX");
		cityIdSelect = DatadicContext.getInstance()
				.getDicDataListByCode("CITY");
		statusSelect = DatadicContext.getInstance()
				.getDicDataListByCode("SBZT");
		// 查询
		equip = equipService.detailEquip(equip);
		//
		lineIdSelect = DatadicContext.getInstance().getDicDataListByCode(
				DatadicContext.getInstance().getDatadicObject(null, equip.getCityId())
						.getClasscode());
		stationSelect = DatadicContext.getInstance().getDicDataListByCode(
				DatadicContext.getInstance().getDatadicObject(null, equip.getLineId())
						.getClasscode());
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit EquipAction initModify() method");
		}
		return SUCCESS;
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public String modify() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter EquipAction modify() method");
		}
		//
		Equip modifyEquip = new Equip();
		modifyEquip.setId(id);
		modifyEquip.setDiscriminate(discriminate);
		modifyEquip.setSerialNum(serialNum);
		modifyEquip.setDeviceType(deviceType);
		modifyEquip.setInstallMan(installMan);
		modifyEquip.setInstallTime(installTime);
		modifyEquip.setSamId(samId);
		modifyEquip.setSamType(samType);
		modifyEquip.setCityId(cityId);
		modifyEquip.setLineId(lineId);
		modifyEquip.setStation(station);
		modifyEquip.setSupplier(supplier);
		modifyEquip.setPurchaseDate(purchaseDate);
		modifyEquip.setRegisterMan(registerMan);
		modifyEquip.setRegisterTime(registerTime);
		modifyEquip.setStatus(status);
		modifyEquip.setComments(comments);
		// 编辑
		equipService.modifyEquip(modifyEquip);
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit EquipAction modify() method");
		}
		return SUCCESS;
	}

	/**
	 * 详情
	 * 
	 * @return
	 */
	public String detail() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter EquipAction detail() method");
		}
		// 查询
		equip = equipService.detailEquip(equip);
		//
		equip.setDeviceTypeText(DatadicContext.getInstance().getDatadicObject(null, equip.getDeviceType()).getDatavalue());
		equip.setCityIdText(DatadicContext.getInstance().getDatadicObject(null, equip.getCityId()).getDatavalue());
		equip.setLineIdText(DatadicContext.getInstance().getDatadicObject(null, equip.getLineId()).getDatavalue());
		equip.setStationText(DatadicContext.getInstance().getDatadicObject(null, equip.getStation()).getDatavalue());
		equip.setSamTypeText(DatadicContext.getInstance().getDatadicObject(null, equip.getSamType()).getDatavalue());
		equip.setStatusText(DatadicContext.getInstance().getDatadicObject(null, equip.getStatus()).getDatavalue());
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit EquipAction detail() method");
		}
		return SUCCESS;
	}

	/**
	 * 新增init
	 * 
	 * @return
	 */
	public String initAdd() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter EquipAction initAdd() method");
		}
		// //
		// deviceTypeSelect =
		// DatadicContext.getInstance().findDicDataList("SBLX",
		// null);
		// samTypeSelect = DatadicContext.getInstance().findDicDataList("SAMLX",
		// null);
		// cityIdSelect = DatadicContext.getInstance().findDicDataList("CS",
		// null);
		// lineIdSelect = DatadicContext.getInstance().findDicDataList("XL",
		// null);
		// stationSelect = DatadicContext.getInstance()
		// .findDicDataList("ZT", null);
		// statusSelect = DatadicContext.getInstance().findDicDataList("SBZT",
		// null);
		//
		deviceTypeSelect = DatadicContext.getInstance().getDicDataListByCode(
				"SBLX");
		samTypeSelect = DatadicContext.getInstance().getDicDataListByCode(
				"SAMLX");
		cityIdSelect = DatadicContext.getInstance()
				.getDicDataListByCode("CITY");
		// lineIdSelect = DatadicContext.getInstance()
		// .findDicDataList("LINE", "2");
		// stationSelect =
		// DatadicContext.getInstance().findDicDataList("STATION",
		// "7");
		statusSelect = DatadicContext.getInstance()
				.getDicDataListByCode("SBZT");
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit EquipAction initAdd() method");
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 * 
	 * @return
	 */
	public String add() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter EquipAction add() method");
		}
		//
		Equip addEquip = new Equip();
		addEquip.setDiscriminate(discriminate);
		addEquip.setSerialNum(serialNum);
		addEquip.setDeviceType(deviceType);
		addEquip.setInstallMan(installMan);
		addEquip.setInstallTime(installTime);
		addEquip.setSamId(samId);
		addEquip.setSamType(samType);
		addEquip.setCityId(cityId);
		addEquip.setLineId(lineId);
		addEquip.setStation(station);
		addEquip.setSupplier(supplier);
		addEquip.setPurchaseDate(purchaseDate);
		addEquip.setRegisterMan(registerMan);
		addEquip.setRegisterTime(registerTime);
		addEquip.setStatus(status);
		addEquip.setComments(comments);
		// 新增
		equipService.addEquip(addEquip);
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit EquipAction add() method");
		}
		return SUCCESS;
	}

	//
	public String ajaxGetLine() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter EquipAction ajaxGetLine() method");
		}
		//
		lineIdSelect = DatadicContext.getInstance().getDicDataListByCode(
				DatadicContext.getInstance().getDatadicObject(null, cityId)
						.getClasscode());
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit EquipAction ajaxGetLine() method");
		}
		return SUCCESS;
	}

	//
	public String ajaxGetStation() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter EquipAction ajaxGetStation() method");
		}
		//
		stationSelect = DatadicContext.getInstance().getDicDataListByCode(
				DatadicContext.getInstance().getDatadicObject(null, lineId)
						.getClasscode());
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit EquipAction ajaxGetStation() method");
		}
		return SUCCESS;
	}
}
